package com.btkjgc.service.impl;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.Dictionary;
import com.btkjgc.models.extend.AccountInfoModel;
import com.btkjgc.service.intf.AccountRecordManageService_Intf;
import com.btkjgc.utils.DateUtils;
import com.btkjgc.utils.ExcelExportor;
import com.btkjgc.utils.HibernateUtil;
import com.btkjgc.utils.LogUtils;

@Component
public class AccountRecordManageService_Impl extends BaseService_Impl implements AccountRecordManageService_Intf
{
	static Logger logger = Logger.getLogger(AccountRecordManageService_Impl.class);

	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv, int pageIndex, int totalRecord,
			List<AccountInfoModel> privateInfoList) {
		// TODO Auto-generated method stub
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		mv.addObject("account_info_p", privateInfoList);
		
		//车型列
		String cartypesql = " select ct.name,CONCAT(id,'') 'key' from cartype_t ct where ct.del = 0";
				
		List<Dictionary> cartypeList = HibernateUtil.executeSQLQueryList(cartypesql,Dictionary.class);
		if(cartypeList.size() > 0)
		{
			mv.addObject("cartypeList", cartypeList);
		}
		
		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
		
	}

	@Override
	public void setListData1(HttpServletRequest request, ModelAndView mv, int pageIndex1, int totalRecord1,
			List<AccountInfoModel> companyInfoList) {
		// TODO Auto-generated method stub
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		mv.addObject("account_info_c", companyInfoList);
		
		//公司列
		String companysql = " select aut.name,CONCAT(id,'') 'key' from account_user_t aut where aut.type=1 and aut.del = 0";
				
		List<Dictionary> companyList = HibernateUtil.executeSQLQueryList(companysql,Dictionary.class);
		if (companyList.size() > 0)
		{
			mv.addObject("companyList", companyList);
		}
		
		mv.addObject("currentpage1", pageIndex1);
		mv.addObject("totalRecord1", totalRecord1);
		
	}

	@Override
	public void doDownloadAction(HttpServletRequest request,
			HttpServletResponse response, String sql) {
		// TODO Auto-generated method stub
		ExcelExportor<String[]> exproter = new ExcelExportor<String[]>();
		try
		{
			String entity = null;
			String export =request.getParameter("export");
			if ("1".equals(export)) {
				entity="公司";
			} else if ("0".equals(export)) {
				entity="个人";
			}
			
			response.setHeader("content-disposition", "attachment;filename="
					+ URLEncoder.encode(DateUtils.getDateTime("yyyyMMdd")+entity+"账户记录列表.xls", "UTF-8"));
			OutputStream out = response.getOutputStream();
			
			List<AccountInfoModel> items = HibernateUtil.executeSQLQueryList(sql, AccountInfoModel.class);
			
			List<String[]> rows = new ArrayList<String[]>();
			
			String[] header =  new String[] { "账号", "所属"+entity, "手机号", "说明", "可用余额",
					"返现余额", "累计免费里程", "累计返现", "累计消费", "累计充值", "累计退款", "有效优惠券"};
			
			for(AccountInfoModel item : items)
			{
				String[] row = new String[header.length];
				row[0] = item.getAccount();
				row[1] = item.getName();
				
				row[2] = item.getTel();
				row[3] = item.getComment();
				row[4] = String.valueOf(item.getAvailable_balance()/100);
				
				row[5] = String.valueOf(0);
				row[6] = String.valueOf(item.getTotal_free_trace()/1000);
				row[7] = String.valueOf(item.getTotal_back_balance()/100);
				
				row[8] = String.valueOf(item.getTotal_consum()/100);
				row[9] = String.valueOf(item.getTotal_recharge()/100);
				row[10] = String.valueOf(item.getTotal_refund()/100);
				
				row[11] = String.valueOf(item.getCoupon_num());
				rows.add(row);
			}
			exproter.exportExcel(DateUtils.getDateTime("yyyyMMdd")+entity+"账户记录列表", header, rows, out, null);
		}
		catch (Exception e)
		{
			LogUtils.error(e);
		}
	}
	
}